import java.io.*
import java.util.*

/**
 * @author ldh
 * 时间: 2022/10/13 17:30
 * 邮箱: 2637614077@qq.com
 */
object Transformer {
    private val kwicList = ArrayList<String>()
    private val lineTxt = ArrayList<String>()
    private var inputFile: BufferedReader? = null
    private var outputFile: BufferedWriter? = null

    fun transform(string: String): String {
        lineTxt.clear()
        kwicList.clear()
        string.split('\n').forEach {
            lineTxt.add(it)
        }
        shift()
        alphabetizer()
        return kwicList.joinToString(separator = "") { """
            
            $it
            
        """.trimIndent() }
    }


    fun input(fileName: String?) {
        try {
            inputFile = BufferedReader(FileReader(fileName))
        } catch (e: Exception) {
            e.printStackTrace()
        }
        var line: String
        try {
            while (inputFile!!.readLine().also { line = it } != null) {
                lineTxt.add(line)
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    fun output(filename: String?) {
        val it: Iterator<String> = kwicList.iterator()
        try {
            outputFile = BufferedWriter(FileWriter(filename))
            while (it.hasNext()) {
                outputFile!!.write(
                    """
                    ${it.next()}
                    
                    """.trimIndent()
                )
            }
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            try {
                if (outputFile != null) {
                    outputFile!!.close()
                }
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    }

    fun shift() {
        //获取每个单词，存入tokens
        val it: Iterator<String> = lineTxt.iterator()
        while (it.hasNext()) {
            val token = StringTokenizer(it.next())
            val tokens = ArrayList<String>()
            var i = 0
            //循环添加单词
            val count = token.countTokens()
            while (i < count) {
                tokens.add(token.nextToken())
                i++
            }

            //display(tokens);
            //切割各个单词，不断改变起始值和利用loop实现位移。
            i = 0
            while (i < count) {
                val lineBuffer = StringBuffer()
                var index = i
                for (f in 0 until count) {
                    //从头继续位移
                    if (index >= count) index = 0
                    //存入StringBuffer
                    lineBuffer.append(tokens[index])
                    lineBuffer.append(" ")
                    index++
                }
                val tmp = lineBuffer.toString()
                kwicList.add(tmp)
                i++
            }
        }
    }


    fun alphabetizer() {
        Collections.sort(kwicList, AlphabetizerComparator())
    }

    private class AlphabetizerComparator : Comparator<String?> {
        override fun compare(o1: String?, o2: String?): Int {
            if (o1 == null && o2 == null) {
                throw NullPointerException()
            }
            var compareValue = 0
            val o1c = o1!!.lowercase(Locale.getDefault())[0] //忽略大小写
            val o2c = o2!!.lowercase(Locale.getDefault())[0] //忽略大小写
            compareValue = o1c.code - o2c.code
            return compareValue
        }
    }
}